// index.js
const app = getApp()
import { request } from "../../utils/request"
import config from '../../utils/config'
Page({
    data: {
        custom: null,
        searchBarBackground:'transparent',
        adList:[],
        adType1:null,
        adType2:null,
        path:'/pages/index/index',
        cateHeight:0,
        bannerHeight:0,
        bannerList:[
            {id:1,good_id:0,page:null,page_id:0,type:null,img:"/static/img/banner1.png"},
            {id:1,good_id:0,page:null,page_id:0,type:null,img:"/static/img/banner1.png"},
            {id:1,good_id:0,page:null,page_id:0,type:null,img:"/static/img/banner1.png"},
        ],
        bannerIndex:0,
        navsList:[
            {title:'城市独家',url:'/pages/good_list_type/good_list?type=2',src:config.picUrl + '/static/img/chengshidujia.png'},
            {title:'城市限定',url:'/pages/good_list_type/good_list?type=3',src:config.picUrl + '/static/img/chengshixianding.png'},
            {title:'盒装',url:'/pages/good_list_type/good_list?type=1',src:config.picUrl + '/static/img/hezhuang.png'},
            {title:'服装道具',url:'/pages/good_list_type/good_list?type=5',src:config.picUrl + '/static/img/fuzhuangdaoju.png'},
            {title:'桌游',url:'/pages/good_list_type/good_list?type=4',src:config.picUrl + '/static/img/zhuoyou.png'},
        ],
        picUrl:config.picUrl,
        is_new:1,
        good_type:1,//筛选的商品类型，有re-热门推荐，xin-新剧预热，bi-必备好剧，4-好剧团购四种
        page:1,
        last_page:1,
        leftGood:[],
        rightGood:[],
        endHide:true,
        loading:true,
        visible1:false,
        list24:[],
        list7:[],
        list5:[]
    },
    onLoad(req) {
        if('extension' in req){
            wx.setStorageSync('extension', req.extension)
        }
        wx.showShareMenu({
          menus: ["shareAppMessage","shareTimeline"],
        })
        let query = wx.createSelectorQuery();  //创建节点选择器
        query.select('.banner').boundingClientRect(res=>{
            this.setData({bannerHeight:res.height})
        }).exec();
        this.setData({
            custom:wx.getMenuButtonBoundingClientRect()
        })
        this.getBanner()
        this.getGoodList()
        this.getTop()
        this.getAd()
        this.getBang()
    },
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }
    },
    onReady(){
        let that = this;
        //获取元素节点的位置信息
        const query = wx.createSelectorQuery().in(this)
        //小程序BUG不加延时算出来的高度部分机型不准确，目前官方没有给更好的解决方案
        setTimeout(() => {
            query.select('.cate').boundingClientRect(res=>{
                that.setData({
                    cateHeight: parseInt(res.height)
                })
            }).exec();
        }, 0);
    },
    naviGood(e){
        var info = wx.getStorageSync('userInfo')
        //authentication_type 认证类型3-发行商，4-商户，5-普通访客
        //is_authentication 是否经过系统认证，0-未认证；1-审核中；2-审核通过；3-审核被拒
        if(info.is_wechat_authentication != 1){
            wx.showModal({
                title:'提示',
                content:"您还未登录或注册，请先登录",
                cancelText:"取消",
                confirmText:"登录",
                success:res=>{
                    if(res.confirm){
                        wx.switchTab({url:"/pages/my/my"})
                    }else{

                    }
                }
            })
            return false
        }
        if(info.authentication_type == 5){
            wx.showModal({
                title:'提示',
                content:"您还未进行身份认证，请先认证您身份信息",
                cancelText:"认证商户",
                confirmColor:"#000",
                confirmText:"认证发行",
                success:res=>{
                    if(res.confirm){
                        wx.navigateTo({url:"/pages/faxingrenzheng/faxingrenzheng"})
                    }else{
                        wx.navigateTo({url:"/pages/shanghurenzheng/shanghurenzheng"})
                    }
                }
            })
            return false;
        }
        if(info.is_authentication != 2){
            config.toast("您的身份审核暂未通过，请等待审核通过",201,()=>{})
            return false
        }
        wx.navigateTo({url:e.currentTarget.dataset.url})
    },
    getBanner(){
        request({
            url:'/miniprogram/system/getPageBanner',
            data:{page:this.data.path},
            type:'post'
        }).then(res=>{
            this.setData({
                bannerList:res.data
            })
        })
    },
    bannerChange(e){
        this.setData({bannerIndex:e.detail.current})
        // var dotLeft = e.detail.current * this.data.dotWidth
        // this.setData({dotLeft})
    },
    changeNew(e){
        this.setData({is_new:parseInt(e.currentTarget.dataset.new)})
        this.initGoodList()
    },
    changeType(e){
        this.setData({good_type:e.currentTarget.dataset.type})
        this.initGoodList()
    },
    initGoodList(){
        this.setData({page:1,endHide:true})
        this.getGoodList()
    },
    getAd(){
        request({url:'/miniprogram/system/getAd'}).then(res=>{
            if(res.code != 200){
                return false
            }
            this.setData({
                adType1:res.data.type1,
                adType2:res.data.type2
            })
            if(this.data.adType2 != null){
                this.setData({visible1:true})
            }
        })
	},
	onClose1(){
		this.setData({visible1:false})
	},
    getTop(){
        request({url:'/miniprogram/system/getTop',type:'get'}).then(res=>{
            this.setData({adList:res.data})
        }) 
    },
    getGoodList(){
        request({
            url:"/miniprogram/good/getIndexGood",
            data:{is_new:this.data.is_new,order:this.data.good_type,limit:10,page:this.data.page},
            type:'post'
        }).then(res=>{
            this.setData({
                last_page:res.last_page
            })
            // if(res.data.length == 0){
            //     this.setData({
            //         leftGood:[],
            //         rightGood:[]
            //     })
            // }
            var left = [],
                right = []
            for(let i in res.data){
                if(i%2){
                    right.push(res.data[i])
                }else{
                    left.push(res.data[i])
                }
            }
            if(this.data.page == 1){
                this.setData({leftGood:[],rightGood:[]})
            }
            this.setData({
                leftGood:this.data.leftGood.concat(left),
                rightGood:this.data.rightGood.concat(right)
            })
        })
    },
    onPageScroll: function(e) {
        if(e.scrollTop > 139){
            this.setData({searchBarBackground:'#fff'})
        }else if(e.scrollTop <= 139){
            this.setData({searchBarBackground:'transparent'})
        }
    },
    onReachBottom: function(e) {
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false;
        }

        this.setData({page:this.data.page+1})
        this.getGoodList()
    },
    getBang(){
        request({
            url:'/miniprogram/good/getbang'
        }).then(res=>{
            this.setData({
                list24:res.data.list24,
                list7:res.data.list7,
                list5:res.data.list5
            })
        })
    }
})
